package cn.dengyonsheng.mail_send_service.controllers;

import cn.dengyonsheng.mail_send_service.entities.MailMessage;
import cn.dengyonsheng.mail_send_service.services.MailProducer;
import lombok.extern.jackson.Jacksonized;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/mail")
public class MailSendController {
    private final MailProducer mailProducer;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    public MailSendController(MailProducer mailProducer) {
        this.mailProducer = mailProducer;
    }

    @PostMapping("/send")
    @ResponseBody
    public String sendMail(@RequestBody MailMessage mailMessage) {
        mailProducer.sendMsg(mailMessage.toString());
        logger.info("邮件添加到队列：" + mailMessage.toString());
        return "邮件发送成功";
    }

    @GetMapping("/template")
    @ResponseBody
    @Jacksonized
    public String getTemplate() {
        MailMessage mailMessage = new MailMessage();
        mailMessage.setText("12313");
        mailMessage.setTo("465465@qq.com");
        mailMessage.setText("模板正文");
        return mailMessage.toString();
    }
}
